package com.midea.iot.course1.javaSE3;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 作业3
 * @author xingwuxw
 * 分析以下需求，并用代码实现：  (
 * 1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
 * (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
 * (3)利用四种方式遍历Map集合中的内容，格式：key::value
 */
public class Student {
    private String name;
    private int age;

    public static void main(String[] args) {
        HashMap<Student, String> studentMap = new HashMap<Student,String>();
        studentMap.put(new Student("小武1",20), "广州");
        studentMap.put(new Student("小武2",18), "深圳");
        studentMap.put(new Student("小武3",22), "珠海");
        studentMap.put(new Student("小武4",19), "东莞");
        studentMap.put(new Student("小武5",18), "佛山");
        setStudentMap1(studentMap);
        setStudentMap2(studentMap);
    }
    public static void setStudentMap1(HashMap<Student, String> studentMap) {
        Set<Map.Entry<Student, String>> studentSet = studentMap.entrySet();
        System.out.println("调用studentMap集合方法entrySet方法,用Iterator迭代取出key:");
        Iterator<Map.Entry<Student, String>> studentIt = studentSet.iterator();
        while(studentIt.hasNext()) {
            Map.Entry<Student, String> entry = studentIt.next();
            Student studentKey = entry.getKey();
            String value = entry.getValue();
            System.out.println("\t"+studentKey+"的地址："+value);
        }
        System.out.println("\n调用studentMap集合方法：entrySet方法,用for···each循环取出key:");
        for(Map.Entry<Student, String> me : studentMap.entrySet()) {
            Student studentKey = me.getKey();
            String value = me.getValue();
            System.out.println("\t"+studentKey+"的地址："+value);
        }

    }

    public static void setStudentMap2(HashMap<Student, String> studentMap) {
        Set<Student> studentSet = studentMap.keySet();
        System.out.println("\n调用studentMap集合方法：keySet方法,用Iterator迭代取出key:");
        Iterator<Student> studentIt = studentSet.iterator();
        while(studentIt.hasNext()) {
            Student studentKey = studentIt.next();
            String value = studentMap.get(studentKey);
            System.out.println("\t"+studentKey+"的地址："+value);
        }

        System.out.println("\n调用studentMap集合方法：keySet方法,用for···each循环取出key:");
        for(Student stuSet : studentMap.keySet()) {
            Student studentKey = stuSet;
            String value = studentMap.get(studentKey);
            System.out.println("\t"+studentKey+"的地址："+value);
        }
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return  "Student [姓名：" + name + ", 年龄：" + age + "]";
    }
}
